/*
 
 Uncontended Lock implementation
 Copyright (c) 2007, William Pugh

 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 */
package jsr166.contrib.uncontended;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;


/**
 * This class contains static methods to wrap maps with uncontended  
 * readwrite locks.
 * 
 * @author pugh
 *
 */
final public class Collections {
    private Collections() {}
    /**
     * Wraps a map with an uncontended read write lock
     * @param map to wrap
     * @return wrapped map
     */
    public static <K,V> Map<K,V> checkedUncontendedMap(Map<K,V> map) {
        return readWriteLockedMap(map, new UncontendedReadWriteLock());
    }
    /**
     * Wraps a map with  the provided read write lock
     * @param map to wrap
     * @param rwLock read write lock
     * @return wrapped map
     */
    public static <K,V> Map<K,V> readWriteLockedMap(Map<K,V> map, ReadWriteLock rwLock) {
        return new ReadWriteLockedMap<K, V, Map<K,V>>(map, rwLock);
    }
    /**
     * Wraps a set with an  uncontended read write lock
     * @param set to wrap
     * @return wrapped set
     */
    public static <E> Set<E> checkedUncontendedSet(Set<E> set) {
        UncontendedReadWriteLock rwLock = new UncontendedReadWriteLock();
        return readWriteLockedSet(set, rwLock);
    }
    /**
     * Wraps a set with the provided read write lock
     * @param set to wrap
     * @param rwLock read write lock
     * @return wrapped set
     */
    public static <E> Set<E> readWriteLockedSet(Set<E> set, ReadWriteLock rwLock) {
        return new ReadWriteLockedSet<E>(set, rwLock);
    }
    /**
     * Wraps a list with an  uncontended read write lock
     * @param list the list to wrap
     * @return wrapped list
     */
    public static <E> List<E> checkedUncontendedList(List<E> list) {
        UncontendedReadWriteLock rwLock = new UncontendedReadWriteLock();
        return readWriteLockedList(list, rwLock);
    }
    /**
     * Wraps a list with the provided read write lock
     * @param list the list to wrap
     * @param rwLock read write lock
     * @return wrapped list
     */
    public static <E> ReadWriteLockedList<E> readWriteLockedList(List<E> list, ReadWriteLock rwLock) {
        return new ReadWriteLockedList<E>(list, rwLock);
    }
    /**
     * Wraps a collection with  an  uncontended read write lock
     * @param col the colection to wrap
     * @return wrapped collection
     */
    public static <E> Collection<E> checkedUncontendedCollection(Collection<E> col) {
        UncontendedReadWriteLock rwLock = new UncontendedReadWriteLock();
        return readWriteLockedCollection(col, rwLock);
    }
    /**
     * Wraps a collection with the provided read write lock
     * @param col the colection to wrap
     * @param rwLock read write lock
     * @return wrapped collection
     */
    public static <E> ReadWriteLockedCollection<E, Collection<E>> readWriteLockedCollection(Collection<E> col, ReadWriteLock rwLock) {
        return new ReadWriteLockedCollection<E, Collection<E>>(col, rwLock);
    }

}
